package com.yisoo.ubag.iot.controller;

import java.util.Arrays;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yisoo.ubag.iot.entity.ProdEntity;
import com.yisoo.ubag.iot.service.ProdService;
import com.yisoo.common.utils.PageUtils;
import com.yisoo.common.utils.R;



/**
 * 
 *
 * @author mmciel
 * @email 761998179@qq.com
 * @date 2021-02-09 20:28:44
 */
@RestController
@RequestMapping("iot/prod")
public class ProdController {
    @Autowired
    private ProdService prodService;

    //仅做测试
    @GetMapping("/topic/{pkey}")
    public R topicAdd(@PathVariable("pkey")String productKey){
//        return R.ok();
        return prodService.addTopicAuto(productKey);
    }

    //创建一个订阅，这个接口仅作测试，相关服务已经被同步执行
    @GetMapping("/dingyue/{pkey}")
    public R dingyue(@PathVariable("pkey")String productKey){
//        return R.ok();
        return prodService.addproductSubAndPub(productKey);
    }


    //添加一个产品，并保存到数据库，这个操作在用户开通硬件服务后才调用，远程调用
    @GetMapping("/add/{cid}/{pname}")
    public R Add(@PathVariable("cid")Long cid,@PathVariable("pname")String pname){
//        return R.ok();
        return prodService.addproduct(cid,pname);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("iot:prod:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = prodService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{consumerId}")
    //@RequiresPermissions("iot:prod:info")
    public R info(@PathVariable("consumerId") Long consumerId){
		ProdEntity prod = prodService.getById(consumerId);

        return R.ok().put("prod", prod);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("iot:prod:save")
    public R save(@RequestBody ProdEntity prod){
		prodService.save(prod);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("iot:prod:update")
    public R update(@RequestBody ProdEntity prod){
		prodService.updateById(prod);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("iot:prod:delete")
    public R delete(@RequestBody Long[] consumerIds){
		prodService.removeByIds(Arrays.asList(consumerIds));

        return R.ok();
    }

}
